home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.teleport.com!usenet
- From: GHouck <hksys@teleport.com>
- Newsgroups: comp.lang.c
- Subject: Re: accessing structures (newbie question)
- Date: 20 Feb 1996 10:24:03 GMT
- Organization: systems hk
- Message-ID: <4gc7g3$dm7@maureen.teleport.com>
- References: <4g8gic$o6u@news1.sunbelt.net>
- NNTP-Posting-Host: ip-pdx04-22.teleport.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
-
- dking@SunBelt.Net wrote:
- >struct picture *photos;
- >photos = (struct picture *)malloc(1000 * sizeof(picture));
- >
- >now I try to store data into structures...
- > for (x=0;x<num_of_files;x++)
- > {
- > strncpy(photos->filename, filelist[x],12);
- > strncpy(photos->diskname, inputdisk,12);
- > strncpy(photos->indexname, inputindex,12);
- > }
- >for (x=0;x<num_of_files;x++)
- >{
- >printf("%sdisk%sindex%s\n",photos->filename,photos->diskname,photos->indexname
- >Obviously I'm pointing to the first of the 1000 structures the entire time.
- >Now for the 60,000 dollar question: HOW do I access the rest of the
- >structures? My tutuorial doesnt have any examples of accessing MALLOCed
- >structures. The one example on structures defines it as an array (struct
- >picture *photos[x] ) but 1000 elements is too large an array and the example
- >with malloc uses floating point data accessed by using (photos[x]) which
- >doesnt work either with the strnccpy or with the -> operator.
- >
- Daryl,
-
- Just can dereferemce the structures with a pointer offset or a subscript,
- as below:
-
- (photos+x)->filename, (photos+x)->diskname, ...
- or
- photos[x].filename, photos[x].diskname, ...
-
- The 'x' offset/subscript increments the sizeof(photos) each time.
-
- Yours, Geoff Houck
-
-